﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Xmobile.Libs;
using CNDM.DB;

namespace CNDM.BO.News
{
    public class NewsData
    {
        public static DataTable Get_NoiBatTrangChu(int Top)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_NoiBatTrangChu(Top);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_NoiBatTrangChu(Top);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("Image_Url");
                    dt.Columns.Add("VietNamDate");

                    for (int i = 0; i < count; i++)
                    {

                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);
                        if (dt.Rows[i]["News_Image"].ToString() != "")
                            dt.Rows[i]["Image_Url"] = Xmobile.Libs.Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        else
                            dt.Rows[i]["Image_Url"] = "";
                        /*                    
                        if (dt.Rows[i]["News_Image"] != null && dt.Rows[i]["News_Image"].ToString() != "")
                        {
                            dt.Rows[i]["ImageThumb"] = Utilitis.THUMBNAIL_LINK(dt.Rows[i]["News_Image"].ToString(), 80, 0);
                        }
                        else
                        {
                            dt.Rows[i]["ImageThumb"] = "";
                        }
                        */
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }

        public static DataTable Get_NoiBatChuyenMuc(int Cat_ID, int Top)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_NoiBatChuyenMuc(Cat_ID, Top);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_NoiBatChuyenMuc(Cat_ID, Top);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;
                   
                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");
                    for (int i = 0; i < count; i++)
                    {

                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);
                        if (dt.Rows[i]["News_Image"].ToString() != "")
                            dt.Rows[i]["Image_Url"] = Xmobile.Libs.Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        else
                            dt.Rows[i]["Image_Url"] = "";
                        /*                    
                        if (dt.Rows[i]["News_Image"] != null && dt.Rows[i]["News_Image"].ToString() != "")
                        {
                            dt.Rows[i]["ImageThumb"] = Utilitis.THUMBNAIL_LINK(dt.Rows[i]["News_Image"].ToString(), 80, 0);
                        }
                        else
                        {
                            dt.Rows[i]["ImageThumb"] = "";
                        }
                        */
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }

        public static DataTable Get_DanhSachTin(string News_ID, string Cat_ID, int PageIndex, int PageSize)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_DanhSachTin(News_ID, Cat_ID, PageIndex, PageSize);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_DanhSachTin(News_ID, Cat_ID, PageIndex, PageSize);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");

                    for (int i = 0; i < count; i++)
                    {

                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);
                        if (dt.Rows[i]["News_Image"].ToString() != "")
                            dt.Rows[i]["Image_Url"] = Xmobile.Libs.Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        else
                            dt.Rows[i]["Image_Url"] = "";
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }


        public static DataTable Get_DanhSachTinCu(Int64 News_ID, int Top, int Cat_ID)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_DanhSachTinCu(News_ID, Top, Cat_ID);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_DanhSachTinCu(News_ID, Top, Cat_ID);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");
                    for (int i = 0; i < count; i++)
                    {
                        if (dt.Rows[i]["News_Image"].ToString().Trim() != "")
                        {
                            dt.Rows[i]["Image_Url"] = Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        }
                        else
                        {
                            dt.Rows[i]["Image_Url"] = "";
                        }
                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);

                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }

        public static DataTable Get_DanhSachTinMoi(Int64 News_ID, int Top, int Cat_ID)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_DanhSachTinMoi(News_ID, Top, Cat_ID);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_DanhSachTinMoi(News_ID, Top, Cat_ID);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");
                    
                    for (int i = 0; i < count; i++)
                    {
                        if (dt.Rows[i]["News_Image"].ToString().Trim() != "")
                        {
                            dt.Rows[i]["Image_Url"] = Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        }
                        else
                        {
                            dt.Rows[i]["Image_Url"] = "";
                        }

                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);

                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }

        public static DataTable Get_NewsDetail(Int64 news_id)
        {
            DataTable dt = new DataTable();

            string cacheName = CacheName.Get_NewsDetail(news_id.ToString());

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_NewsDetail(news_id);
                    db.Close();
                    db.Dispose();
                }
                
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("Image_Url");
                    if (dt.Rows[0]["News_Image"].ToString().Trim() != "")
                    {
                        dt.Rows[0]["Image_Url"] = Constants.DomainImage + dt.Rows[0]["News_Image"].ToString();
                    }
                    else
                    {
                        dt.Rows[0]["Image_Url"] = "";
                    }

                    dt.Rows[0]["NewsUrl"] = Xmobile.Libs.Constants.DomainName +  Utility.News_BuildUrl(news_id.ToString(), Xmobile.Libs.Lib.UnicodeToKoDauAndGach(dt.Rows[0]["Cat_Name"].ToString()), dt.Rows[0]["News_Title"].ToString());

                    dt.AcceptChanges();
                }
            }

            return dt;
        }

        public static DataTable Search_News(string keysearch, int PageIndex, int PageSize)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Search_News(keysearch,  PageIndex, PageSize);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Search_News(keysearch, PageIndex, PageSize);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");

                    for (int i = 0; i < count; i++)
                    {

                        dt.Rows[i]["NewsUrl"] = Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);
                        dt.Rows[i]["Image_Url"] = Xmobile.Libs.Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }

        /// <summary>
        /// Danh sach tin moi nhat
        /// </summary>
        /// <param name="Cat_ID">ID Chuyen muc ( 0 : lat tat cac cac chuyen muc)</param>
        /// <param name="Top">So luong tin can lay ra</param>
        /// <returns></returns>
        public static DataTable Get_TinMoiNhat(int Cat_ID, int Top)
        {
            DataTable dt = new DataTable();
            string cacheName = CacheName.Get_TinMoiNhat(Cat_ID, Top);

            dt = CacheSql.GetFromCacheDependency<DataTable>(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.Get_TinMoiNhat(Cat_ID, Top);
                    db.Close();
                    db.Dispose();
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    int count = dt.Rows.Count;

                    dt.Columns.Add("NewsUrl");
                    dt.Columns.Add("ImageThumb");
                    dt.Columns.Add("VietNamDate");
                    dt.Columns.Add("Image_Url");

                    for (int i = 0; i < count; i++)
                    {

                        dt.Rows[i]["NewsUrl"] = "";//Utility.News_BuildUrl(dt.Rows[i]["News_ID"].ToString(), dt.Rows[i]["Cat_DisplayUrl"].ToString(), dt.Rows[i]["News_Title"].ToString());
                        dt.Rows[i]["VietNamDate"] = String.Format("{0:dd/MM/yyyy HH:mm}", dt.Rows[i]["News_PublishDate"]);
                        if (dt.Rows[i]["News_Image"].ToString() != "")
                            dt.Rows[i]["Image_Url"] = Xmobile.Libs.Constants.DomainImage + dt.Rows[i]["News_Image"].ToString();
                        else
                            dt.Rows[i]["Image_Url"] = "";
                    }

                    dt.AcceptChanges();
                }
            }

            CacheSql.SaveToCacheDependency(CacheName.Database, CacheName.TBL_NewsPublished, cacheName, dt);

            return dt;
        }
    }
}
